/*
This file is part of IRCaBot.
IRCaBot is IRC logger with features.
Source code: https://notabug.org/acetone/ircabot.
Copyright (C) acetone, 2023.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

#include "httprequesttochatlog.h"
#include "httpserver.h"
#include "dataparser.h"
#include "ircloggerlist.h"

HttpRequestToChatLog::HttpRequestToChatLog(const QString& url)
{
    const QString network = parser::get(url, HTTP_URI_CHATLOG_PARAM_NETWORK, parser::GetType::Url);
    if (network.isEmpty())
    {
        m_document.setCode(http::CODE_400);
        m_document.setBodyStandardError("Network name is undefined", "Bad request");
        return;
    }

    const QString channel = parser::get(url, HTTP_URI_CHATLOG_PARAM_CHANNEL, parser::GetType::Url);
    if (channel.isEmpty())
    {
        m_document.setCode(http::CODE_400);
        m_document.setBodyStandardError("Channel name is undefined", "Bad request");
        return;
    }

    m_logger = IRCLoggerList::getConst(network, channel);
    if (m_logger == nullptr)
    {
        m_document.setCode(http::CODE_404);
        m_document.setBodyStandardError("Requested log not found: <b>"
                                        "<p>"+network+"</p>"
                                        "<p>"+channel+"</p>"
                                        "</b>", "Not found");
        return;
    }

    m_year = parser::get(url, HTTP_URI_CHATLOG_PARAM_YEAR, parser::GetType::Url).toUShort();
    m_month = parser::get(url, HTTP_URI_CHATLOG_PARAM_MONTH, parser::GetType::Url).toUShort();
    m_day = parser::get(url, HTTP_URI_CHATLOG_PARAM_DAY, parser::GetType::Url).toUShort();

    if (m_year == 0)
    {
        onRoot();
    }
    else if (m_month == 0)
    {
        onYear();
    }
    else if (m_day == 0)
    {
        onMonth();
    }
    else
    {
        onDay();
    }
}

void HttpRequestToChatLog::onRoot()
{
    QString years = "Years: ";
    foreach(auto& year, m_logger->dateIndex())
    {
        years += QString::number(year.first) + " ";
    }
    m_document.setBody(years.toUtf8());
}

void HttpRequestToChatLog::onYear()
{
    foreach(auto& year, m_logger->dateIndex())
    {
        if (year.first == m_year)
        {
            QString months = "Months: ";
            foreach(auto& month, year.second)
            {
                months += QString::number(month.first) + " ";
            }
            m_document.setBody(months.toUtf8());
            return;
        }
    }
    m_document.setCode(http::CODE_404);
    m_document.setBodyStandardError("Requested year not found", "Not found");
}

void HttpRequestToChatLog::onMonth()
{
    foreach(auto& year, m_logger->dateIndex())
    {
        if (year.first == m_year)
        {
            foreach(auto& month, year.second)
            {
                if (month.first == m_month)
                {
                    QString days = "Days: ";
                    foreach(auto& day, month.second)
                    {
                        days += QString::number(day) + " ";
                    }
                    m_document.setBody(days.toUtf8());
                    return;
                }
            }
        }
    }
    m_document.setCode(http::CODE_404);
    m_document.setBodyStandardError("Requested log not found", "Not found");
}

void HttpRequestToChatLog::onDay()
{
    const QDateTime date = QDateTime::fromString(QString::number(m_year)+"-"+QString::number(m_month)+"-"+QString::number(m_day), "yyyy-M-d");
    if (not date.isValid())
    {
        m_document.setCode(http::CODE_400);
        m_document.setBodyStandardError("Incorrect date", "Bad request");
        return;
    }

    auto messages = m_logger->getMessages(date);
    if (messages.isEmpty())
    {
        m_document.setCode(http::CODE_404);
        m_document.setBodyStandardError("Requested log not found", "Not found");
        return;
    }

    QString log;
    foreach (auto& msg, messages)
    {
        log += "[" + msg->sender() + "] " + msg->text() + "\t" + QString::number(msg->number()) + "\n";
    }
    m_document.setBody(log.toUtf8());
}
